package hotgammon.domain;

import java.util.Iterator;

public class GameImpl_BACKUP2 implements Game{
	protected Board board;
	protected StrategyMove moveStrategy;
	private StrategyDice diceStrategy;
	private StrategyWinner winnerStrategy;
	Color playerInTurn;
	int numberOfMovesLeft;
	int diceThrownCount;

	public GameImpl_BACKUP2(Factory factory){
		board = new StandardBoard();
		playerInTurn = null;		//starts at null to make the initialization of the game easier...
		numberOfMovesLeft = 2;
		diceThrownCount = 0;
		this.moveStrategy = factory.createMoveStrategy();
		this.diceStrategy = factory.createDiceStrategy();
		this.winnerStrategy = factory.createWinnerStrategy();
	}

	public void newGame() {
		board.reset();
		while(playerInTurn==null) nextTurn();
	}

	public void nextTurn() {
		if(playerInTurn==null){
			diceThrown();
			if(lastDiceThrown[0]>lastDiceThrown[1]){
				playerInTurn = Color.RED;
			}else if(lastDiceThrown[0]<lastDiceThrown[1]){
				playerInTurn = Color.BLACK;
			}
		}else{
			diceThrown();
			if(playerInTurn.equals(Color.BLACK)){
				playerInTurn = Color.RED;
			}else if(playerInTurn.equals(Color.RED)){
				playerInTurn = Color.BLACK;
			}
			
		}
		fireBoardChange();
	}

	public boolean move(Location from, Location to) {
		if(moveStrategy.move(board, this, from, to, false)){
			numberOfMovesLeft -= 1;
			if(numberOfMovesLeft==0){
				nextTurn();
			} 
			fireBoardChange(); 
			return true;
		}
		return false;
	}

	public Color getPlayerInTurn() {
		return playerInTurn;
	}

	public int getNumberOfMovesLeft() {
		return numberOfMovesLeft;
	}

	protected int[] lastDiceThrown = {0,0};
	private int[] diceValuesLeft = {0,0};
	public int[] diceThrown() {
		diceThrownCount++;
		
		lastDiceThrown= diceStrategy.diceThrown();
		if(lastDiceThrown[0]==lastDiceThrown[1]){
			numberOfMovesLeft = 4;
		}else{
			numberOfMovesLeft = 2;
		}
		if(lastDiceThrown[0]==lastDiceThrown[1]){
			int[] dice = {lastDiceThrown[0],lastDiceThrown[0],lastDiceThrown[0],lastDiceThrown[0]};
			diceValuesLeft = dice;
		}else{
			diceValuesLeft = lastDiceThrown;
		}
		fireDieRollChange();
		return lastDiceThrown;
	}
	public int[] getLastDiceThrown(){
		return lastDiceThrown;
	}

	public int[] diceValuesLeft() {
			return diceValuesLeft;
	}
	public void setDiceValuesLeft(int[] diceValuesLeft) {
		this.diceValuesLeft = diceValuesLeft;
	}

	public Color winner() {
		return winnerStrategy.winner(diceThrownCount,this);
	}

	public Iterator<Location> boardIterator() {
		return board.iterator();
	}

	public Color getColor(Location location) {
		return board.getColor(location);
	}

	public int getCount(Location location) {
		return board.getCount(location);
	}
	


	public void fireDieRollChange() {
		if (gameListener != null)
			gameListener.diceRolled();
	}

	public void fireBoardChange() {
		if (gameListener != null)
			gameListener.boardChange();
	}

	private GameListener gameListener = null;
	public void addGameListener(GameListener gl){
		gameListener = gl;
	}

	public Object getBoard() {
		// TODO Auto-generated method stub
		return null;
	}

	public Object getMoveStrategy() {
		// TODO Auto-generated method stub
		return null;
	}

}